package com.hk.springtrains.jms;

import org.apache.activemq.command.ActiveMQTopic;
import org.apache.activemq.spring.ActiveMQConnectionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.core.JmsTemplate;

import javax.jms.ConnectionFactory;
import javax.jms.Destination;

@Configuration
@ComponentScan(value = {"com.hk.springtrains.jms"})
public class Config {



    /**
     * 创建连接工厂(提供连接到消息代理的信息)
     */
    @Bean
    public ActiveMQConnectionFactory connectionFactory(){
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
        connectionFactory.setBrokerURL("tcp://localhost:61616");
        return connectionFactory;
    }


    /**
     * 创建消息目的地
     * @return
     */
    @Bean
    public Destination activeMQQueue(){
        Destination destination = null;
        //destination = new ActiveMQQueue("SpringTrains.queue");
        destination = new ActiveMQTopic("SpringTrains.topic");
        return destination;
    }



    @Bean
    public JmsTemplate jmsTemplate(ConnectionFactory connectionFactory,Destination destination){
        JmsTemplate template = new JmsTemplate(connectionFactory);
        template.setDefaultDestination(destination);
        return template;
    }













}
